package com.hsnn.medstgmini.sys.model;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.springframework.format.annotation.DateTimeFormat;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.hsnn.medstgmini.base.std.model.HospInfo;
import com.hsnn.medstgmini.base.std.model.StdCompinfo;
import com.hsnn.medstgmini.model.Status;
import com.hsnn.medstgmini.model.Whether;
import com.hsnn.medstgmini.util.ParseDate;

/**
 * @category 用户
 * @author 蔡春龙
 * @date 2015年6月12日
 */
@Entity
@Table(name = "sys_user")
public class SysUser implements Serializable {

	private static final long serialVersionUID = 3832626162173359411L;

	private String id;
	private String orgId;
	private String username;
	private String password;
	private String confirmPassword;
	private String passwordHint;
	private Date acctExpired;
	private Date passwordExpired;
	private Integer acctEnabled;
	private String email;
	private String name;
	private String phone;
	private Integer locked; // 锁定帐号(0：未锁，1：锁定)
	private Integer type; // 用户类型（1:中心、2:生产企业、3:配送企业、4:医疗机构、5:专家、6:卫生局）
	private String typeName;
	private Integer main; // 是否是主帐号 (0：否，1：是)
	private String bussinessShow; 
	private String pid;
	private Integer status;
	private String statusName;
	private Date loginTime;
	private String loginIp;
	private Integer loginCount;
	private String createUserId;
	private Date createDateTime;
	private String updUser;
	private Date updDateTime;
	private String remark;
	private String defaultProjId;
	private Float bodyHeight;
	private Float weight;
	private Float waist;
	private Date resetTime;
	
	private String acctExpiredStr;
	private String resetTimeStr;
	
	@Transient
	public String getResetTimeStr() {
		if (null != resetTime){
			return ParseDate.parseShortFormat(resetTime);
		}
		return resetTimeStr;
	}

	public void setResetTimeStr(String resetTimeStr) {
		this.resetTimeStr = resetTimeStr;
	}

	@Transient
	public String getAcctExpiredStr() {
		if (null != acctExpired){
			return ParseDate.parseShortFormat(acctExpired);
		}
		return acctExpiredStr;
	}

	public void setAcctExpiredStr(String acctExpiredStr) {
		this.acctExpiredStr = acctExpiredStr;
	}

	@Column(name = "body_height")
	public Float getBodyHeight() {
		return bodyHeight;
	}

	public void setBodyHeight(Float bodyHeight) {
		this.bodyHeight = bodyHeight;
	}
	@Column(name = "weight")
	public Float getWeight() {
		return weight;
	}

	public void setWeight(Float weight) {
		this.weight = weight;
	}
	@Column(name = "waist")
	public Float getWaist() {
		return waist;
	}

	public void setWaist(Float waist) {
		this.waist = waist;
	}

	@Column(name = "reset_time")
	public Date getResetTime() {
		return resetTime;
	}

	public void setResetTime(Date resetTime) {
		this.resetTime = resetTime;
	}

	// 医疗机构、生产企业、配送企业信息
	private Object info;

	// 主帐号
	private SysUser rootUser;

	private String showName;

	@Transient
	private List<SysRole> roleList;

	@Transient
	private SysRole role;

	@Transient
	List<SysPrivilege> privilegeList;

	@Id
	@Column(name = "id")
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Column(name = "username")
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	@Column(name = "password")
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Transient
	public String getConfirmPassword() {
		return confirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

	@Column(name = "password_hint")
	public String getPasswordHint() {
		return passwordHint;
	}

	public void setPasswordHint(String passwordHint) {
		this.passwordHint = passwordHint;
	}

	@DateTimeFormat(pattern = "yyyy-MM-dd")
	@Column(name = "acct_expired")
	public Date getAcctExpired() {
		return acctExpired;
	}

	public void setAcctExpired(Date acctExpired) {
		this.acctExpired = acctExpired;
	}

	@Column(name = "password_expired")
	public Date getPasswordExpired() {
		return passwordExpired;
	}

	public void setPasswordExpired(Date passwordExpired) {
		this.passwordExpired = passwordExpired;
	}

	@Column(name = "acct_enabled")
	public Integer getAcctEnabled() {
		return acctEnabled;
	}

	public void setAcctEnabled(Integer acctEnabled) {
		this.acctEnabled = acctEnabled;
	}

	@Column(name = "email")
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Column(name = "name")
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "phone")
	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	@Column(name = "locked")
	public Integer getLocked() {
		return locked;
	}

	public void setLocked(Integer locked) {
		this.locked = locked;
	}

	@Column(name = "type")
	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	@Transient
	public String getTypeName() {
		if (type != null) {
			typeName = UserType.getName(type);
		}
		return typeName;
	}

	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}

	@Column(name = "pid")
	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	@Column(name = "main")
	public Integer getMain() {
		return main;
	}

	public void setMain(Integer main) {
		this.main = main;
	}

	@Column(name = "status")
	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	@Transient
	public String getStatusName() {
		if (status != null) {
			statusName = Status.getName(status);
		}
		return statusName;
	}

	public void setStatusName(String statusName) {
		this.statusName = statusName;
	}

	@Column(name = "login_time")
	public Date getLoginTime() {
		return loginTime;
	}

	public void setLoginTime(Date loginTime) {
		this.loginTime = loginTime;
	}

	@Column(name = "login_ip")
	public String getLoginIp() {
		return loginIp;
	}

	public void setLoginIp(String loginIp) {
		this.loginIp = loginIp;
	}

	@Column(name = "login_count")
	public Integer getLoginCount() {
		return loginCount;
	}

	public void setLoginCount(Integer loginCount) {
		this.loginCount = loginCount;
	}

	@Column(name = "create_user_id")
	public String getCreateUserId() {
		return createUserId;
	}

	public void setCreateUserId(String createUserId) {
		this.createUserId = createUserId;
	}

	@Column(name = "create_datetime")
	public Date getCreateDateTime() {
		return createDateTime;
	}

	public void setCreateDateTime(Date createDateTime) {
		this.createDateTime = createDateTime;
	}

	@Column(name = "upd_user")
	public String getUpdUser() {
		return updUser;
	}

	public void setUpdUser(String updUser) {
		this.updUser = updUser;
	}

	@Column(name = "upd_datetime")
	public Date getUpdDateTime() {
		return updDateTime;
	}

	public void setUpdDateTime(Date updDateTime) {
		this.updDateTime = updDateTime;
	}

	@Column(name = "remark")
	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	@Transient
	public List<SysRole> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<SysRole> roleList) {
		this.roleList = roleList;
	}

	@Transient
	public SysRole getRole() {
		if (roleList != null && !roleList.isEmpty()) {
			return roleList.get(0);
		}
		return role;
	}

	public void setRole(SysRole role) {
		this.role = role;
	}

	@Transient
	public List<SysPrivilege> getPrivilegeList() {
		return privilegeList;
	}

	public void setPrivilegeList(List<SysPrivilege> privilegeList) {
		this.privilegeList = privilegeList;
	}

	@SuppressWarnings("unchecked")
	@Transient
	public <T> T getInfo() {
		if (info != null) {
			return (T) info;
		}
		return null;
	}

	public void setInfo(Object info) {
		this.info = info;
	}
	@Column(name = "default_proj_id")
	public String getDefaultProjId() {
		return defaultProjId;
	}

	public void setDefaultProjId(String defaultProjId) {
		this.defaultProjId = defaultProjId;
	}

	@Transient
	public SysUser getRootUser() {
		return rootUser;
	}

	public void setRootUser(SysUser rootUser) {
		this.rootUser = rootUser;
	}

	@JsonIgnore
	@Transient
	public String getShowName() {
		if (UserType.zx.getKey() == type) {
			if (Whether.yes.getKey() == main) {
				showName = name;
			} else if (rootUser != null) {
				showName = rootUser.getName();
			}
		} else if (info != null && UserType.sc.getKey() == type ) {
			showName = ((StdCompinfo) info).getCompName();
		}else if( info != null && UserType.ps.getKey() == type){
			showName = ((StdCompinfo) info).getCompName();
		}else if (UserType.yy.getKey() == type && info != null) {
			showName = ((HospInfo) info).getHospName();
		} else {
			showName = name;
		}
		return showName;
	}

	public void setShowName(String showName) {
		this.showName = showName;
	}
	@Column(name = "org_id")
	public String getOrgId() {
		return orgId;
	}

	public void setOrgId(String orgId) {
		this.orgId = orgId;
	}
	@Column(name = "bussiness_show")
	public String getBussinessShow() {
		return bussinessShow;
	}

	public void setBussinessShow(String bussinessShow) {
		this.bussinessShow = bussinessShow;
	}

	@Override
	public String toString() {
		return "SysUser [id=" + id + ", username=" + username + ", password="
				+ "XXXXXX" + ", confirmPassword=" + "XXXXXX"
				+ ", passwordHint=" + "XXXXXX" + ", acctExpired="
				+ acctExpired + ", passwordExpired=" + passwordExpired
				+ ", acctEnabled=" + acctEnabled + ", email=" + email
				+ ", name=" + name + ", phone=" + phone + ", locked=" + locked
				+ ", type=" + type + ", typeName=" + typeName + ", main="
				+ main + ", pid=" + pid + ", status=" + status
				+ ", statusName=" + statusName + ", loginTime=" + loginTime
				+ ", loginIp=" + loginIp + ", loginCount=" + loginCount
				+ ", createUserId=" + createUserId + ", createDateTime="
				+ createDateTime + ", updUser=" + updUser + ", updDateTime="
				+ updDateTime + ", remark=" + remark + ", defaultProjId="
				+ defaultProjId + "]";
	}

}